package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.PaperListDao;
import com.woniuxy.entity.PaperAttribute;
import com.woniuxy.entity.PaperClassEntity;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.PaperListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PaperListServiceImpl implements PaperListService {
    @Autowired(required = false)
    PaperListDao paperListDao;

    @Override
    public ResponseEntity findAll() {
      List<PaperClassEntity> list = paperListDao.findAll();
        return new ResponseEntity<>(200,"success", list);
    }

    @Override
    public ResponseEntity showAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        //查询全部题目
        List<PaperAttribute> list = paperListDao.findPaperAll();

        PageInfo<PaperAttribute> pageInfo = PageInfo.of(list);

        return new ResponseEntity(200, "查询成功", pageInfo);
    }

    @Override
    public ResponseEntity findLike(PaperAttribute paperAttribute, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        if (paperAttribute.getFid() == 0 ) {
            //没有fid，说明直接查询，否则是点击了左边的分类再查询
            List<PaperAttribute> list = paperListDao.findLike(paperAttribute);
            PageInfo<PaperAttribute> pageInfo = PageInfo.of(list);
            return new ResponseEntity(200, "查询成功", pageInfo);
        }else {
            List<PaperAttribute> list = paperListDao.findLikeAndFid(paperAttribute);
            PageInfo<PaperAttribute> pageInfo = PageInfo.of(list);
            return new ResponseEntity(200, "查询成功", pageInfo);
        }
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        //软删除，将is_deleted 字段改为0
        int i = paperListDao.updateOne(id);
        if (i != 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }

    @Override
    public ResponseEntity delete(Integer[] arr) {
        int i = paperListDao.update(arr);
        if (i != 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }
}
